package com.crm.controller.system.authority;

import cn.hutool.core.bean.BeanUtil;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.SysMenuDto;
import com.crm.domain.entity.SysMenu;
import com.crm.domain.query.MenuQuery;
import com.crm.service.system.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    /**
     * 获取菜单列表
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(MenuQuery query){
            List<SysMenu> list = menuService.queryAll(query);
            return ResponseResult.success(list);
    }

    /**
     * 根据id查询菜单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getById(@PathVariable("id") Long id){
        SysMenu menu = menuService.lambdaQuery().eq(SysMenu::getMenuId, id).one();
        return ResponseResult.success(menu);
    }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id")Long id){
        menuService.lambdaUpdate().eq(SysMenu::getMenuId,id).remove();
        return ResponseResult.success();
    }

    /**
     * 新增菜单
     * @param menuDto
     * @return
     */
    @PostMapping
    public ResponseResult saveMenu(@RequestBody SysMenuDto menuDto){
        SysMenu sysMenu = BeanUtil.copyProperties(menuDto, SysMenu.class);
        menuService.save(sysMenu);
        return ResponseResult.success();
    }
    /**
     * 获取菜单下拉树列表
     * @return
     */
    /*@GetMapping("/treeselect")
    public ResponseResult treeSelect(){
        List<TreeItemVo> treeVo = menuService.treeSelect();
        return ResponseResult.success(treeVo);
    }*/

    /**
     * 修改菜单
     * @param menuDto
     * @return
     */
    @PutMapping
    public ResponseResult updateMenu(@RequestBody SysMenuDto menuDto){
        menuService.updateMenu(menuDto);
        return ResponseResult.success();
    }
}
